﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using AEM.Data;
using Actions = AEM.Data.BLL.Actions;

namespace AEM.Controls
{
    /// <summary>
    /// ConnectionState.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectionState : UserControl
    {
        private Point _startPoint;
        private int stepUnit = 5;
        public ConnectionState()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.TitleBar.MouseMove += new MouseEventHandler(TitleBar_MouseMove);
            this.sys_button_close.Click += new RoutedEventHandler(sys_button_close_Click);
            this.Loaded += new RoutedEventHandler(ConnectionState_Loaded);
        }

        void ConnectionState_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeStep();
            Actions.LiftInfo.ResetConnectionState = doReset;
        }
        private void doReset()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                barNormal.Tag = barFault.Tag = barOverhaul.Tag = barFire.Tag = barDriver.Tag = barLocked.Tag = barNC.Tag = null;
                countNormal.Text = countFault.Text = countOverhaul.Text = countFire.Text = countDriver.Text = countLocked.Text = countNormal.Text = "0";
                InitializeStep();
            }));
        }

        private void InitializeStep()
        {
            if (stepUnit == 5)
            {
                try
                {
                    if (null != Config.LiftInftList)
                    {
                        stepUnit = (int)Math.Floor(200.00 / Config.LiftInftList.Count);
                    }
                }
                catch { }
            }
        }

        public void AddState(int liftnum, Data.Enum.LiftStatus status)
        {
            switch (status)
            {
                case Data.Enum.LiftStatus.Fault:
                    AddLift(barFault, countFault, liftnum);
                    break;
                case Data.Enum.LiftStatus.Overhaul:
                    AddLift(barOverhaul, countOverhaul, liftnum);
                    break;
                case Data.Enum.LiftStatus.Locked:
                    AddLift(barLocked, countLocked, liftnum);
                    break;
                case Data.Enum.LiftStatus.Fire:
                    AddLift(barFire, countFire, liftnum);
                    break;
                case Data.Enum.LiftStatus.Driver:
                    AddLift(barDriver, countDriver, liftnum);
                    break;
                case Data.Enum.LiftStatus.NC:
                    AddLift(barNC, countNC, liftnum);
                    break;
                case Data.Enum.LiftStatus.Normal:
                default:
                    AddLift(barNormal, countNormal, liftnum);
                    break;
            }
        }

        private void AddLift(Border bar, TextBlock txt, int liftnum)
        {
            InitializeStep();
            if (!Exist(bar.Tag, liftnum))
            {
                List<int> ls = (List<int>)bar.Tag;
                if (null == ls) ls = new List<int>();
                ls.Add(liftnum);
                bar.Tag = ls;
                int count = ls.Count;
                int barwidth = count * stepUnit;
                bar.Width = barwidth > 5 ? barwidth + 5 : 5;
                txt.Text = count.ToString();
            }
        }

        private bool Exist(object tag, int liftnum)
        {
            List<int> ls = (List<int>)tag;
            if (null == ls) return false;
            int index = ls.FindIndex(x => x == liftnum);
            if (-1 != index) return true;
            return false;
        }

        public void DeleteState(int liftnum, Data.Enum.LiftStatus status)
        {
            switch (status)
            {
                case Data.Enum.LiftStatus.Fault:
                    DeleteLift(barFault, countFault, liftnum);
                    break;
                case Data.Enum.LiftStatus.Overhaul:
                    DeleteLift(barOverhaul, countOverhaul, liftnum);
                    break;
                case Data.Enum.LiftStatus.Locked:
                    DeleteLift(barLocked, countLocked, liftnum);
                    break;
                case Data.Enum.LiftStatus.Fire:
                    DeleteLift(barFire, countFire, liftnum);
                    break;
                case Data.Enum.LiftStatus.Driver:
                    DeleteLift(barDriver, countDriver, liftnum);
                    break;
                case Data.Enum.LiftStatus.NC:
                    DeleteLift(barNC, countNC, liftnum);
                    break;
                case Data.Enum.LiftStatus.Normal:
                    DeleteLift(barNormal, countNormal, liftnum);
                    break;
            }
        }

        private void DeleteLift(Border bar, TextBlock txt, int liftnum)
        {
            InitializeStep();
            if (Exist(bar.Tag, liftnum))
            {
                int count = 0;
                List<int> ls = (List<int>)bar.Tag;
                if (null != ls)
                {
                    ls.Remove(liftnum);
                    bar.Tag = ls;
                    count = ls.Count;
                }
                int barwidth = count * stepUnit;
                bar.Width = barwidth > 5 ? barwidth + 5 : 5;
                txt.Text = count.ToString();

            }
        }

        public void CountState(SerialDataEventArgs e)
        {
            if (null == e || null == e.Data) return;
            DeleteState(e.LiftNum, Data.Enum.LiftStatus.NC);

            if (e.Data.FaultLamp) AddState(e.LiftNum, Data.Enum.LiftStatus.Fault);
            else DeleteState(e.LiftNum, Data.Enum.LiftStatus.Fault);

            if (e.Data.Overhaul) AddState(e.LiftNum, Data.Enum.LiftStatus.Overhaul);
            else DeleteState(e.LiftNum, Data.Enum.LiftStatus.Overhaul);

            if (e.Data.Locked) AddState(e.LiftNum, Data.Enum.LiftStatus.Locked);
            else DeleteState(e.LiftNum, Data.Enum.LiftStatus.Locked);

            if (e.Data.Fire) AddState(e.LiftNum, Data.Enum.LiftStatus.Fire);
            else DeleteState(e.LiftNum, Data.Enum.LiftStatus.Fire);

            if (e.Data.Dirver) AddState(e.LiftNum, Data.Enum.LiftStatus.Driver);
            else DeleteState(e.LiftNum, Data.Enum.LiftStatus.Driver);

            if (e.Data.FaultLamp || e.Data.Overhaul || e.Data.Locked || e.Data.Fire || e.Data.Dirver)
                DeleteState(e.LiftNum, Data.Enum.LiftStatus.Normal);
            else
                AddState(e.LiftNum, Data.Enum.LiftStatus.Normal);
        }

        public void AddStateNC(int liftnum)
        {
            AddState(liftnum, Data.Enum.LiftStatus.NC);
            DeleteState(liftnum, Data.Enum.LiftStatus.Normal);
            DeleteState(liftnum, Data.Enum.LiftStatus.Fault);
            DeleteState(liftnum, Data.Enum.LiftStatus.Overhaul);
            DeleteState(liftnum, Data.Enum.LiftStatus.Locked);
            DeleteState(liftnum, Data.Enum.LiftStatus.Fire);
            DeleteState(liftnum, Data.Enum.LiftStatus.Driver);
        }

        #region 拖动功能
        private void StartDrag(MouseEventArgs e)
        {
            Point thisPoint = e.GetPosition(null);
            double left = 0, top = 0;
            left = this.Margin.Left;
            top = this.Margin.Top;
            left = thisPoint.X - _startPoint.X + left;
            top = thisPoint.Y - _startPoint.Y + top;
            double maxtop = System.Windows.SystemParameters.WorkArea.Height - this.Height;
            double maxleft = System.Windows.SystemParameters.WorkArea.Width - this.Width;
            if (left > maxleft) left = maxleft;
            if (top > maxtop) top = maxtop;
            if (left < 10) left = 10;
            if (top < 10) top = 10;
            this.Margin = new Thickness(left, top, 0, 0);

            _startPoint = thisPoint;
        }

        void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point position = e.GetPosition(null);
                if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    this.StartDrag(e);
                }
            }
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }
        #endregion

        void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            Close.Invoke(this, new EventArgs());
        }

        public EventHandler Close;

    }
}
